<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="text" id="inputID">
    <input type="button" onclick="Foo()" name="Test" value="测试">
    </br>
    <label>输出结果：</label>
    <label id="labelID">_______</label>
    <script>
        window.onload = function () {
            window.Foo = function () {
                var inputValue = document.getElementById("inputID").value;
                try {
                    var n = parseInt(inputValue);
                    var a = new Array(n); //定义一个数组 传3试试、再传-5试试
                    //报错之后 for循环不会执行
                    for (var i = 0; i < n; i++) {
                        a[i] = i;
                    }
                } catch (e) {
                    // alert(e.name + e.message);
                    alert("请输入正确的数字");
                    console.log("n:", n);
                    a = [];
                } finally {
                    document.getElementById("labelID").innerHTML = a + "_" + (a instanceof Array)
                }
            };
        };
    </script>
</body>

</html>